package com.ck.controller;


import com.ck.util.VideoUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

@Slf4j
@Controller
public class VideoController {
    /**
     * 播放加密MP4
     * @param response
     */
    @RequestMapping("video/playMp4")
    @ResponseBody
    public void playMp4(HttpServletResponse response, String fileName) {
        String filePath = "E:\\_ck\\素材\\";
        // TODO 测试联调结束后需删除
        fileName = "test.mp4";
        String tempFilePath = filePath +  fileName;
        try {

            VideoUtils.decrypt(filePath+fileName,filePath+"666.mp4",4);

            tempFilePath = filePath+"666.mp4";
            // 解密过后的临时文件路径

            FileInputStream inputStream = new FileInputStream(tempFilePath);
            byte[] data = VideoUtils.inputStreamToByte(inputStream);
            String diskfilename = "final.mp4";
            response.setContentType("video/mp4");
            response.setHeader("Content-Disposition", "attachment; filename=\"" + diskfilename + "\"" );
            System.out.println("data.length " + data.length);
            response.setContentLength(data.length);
            response.setHeader("Content-Range", "" + Integer.valueOf(data.length-1));
            response.setHeader("Accept-Ranges", "bytes");
            response.setHeader("Etag", "W/\"9767057-1323779115364\"");
            OutputStream os = response.getOutputStream();

            os.write(data);
            //先声明的流后关掉！
            os.flush();
            os.close();
            inputStream.close();

        }catch (Exception e){
            log.error("....", e);
        }finally {
            VideoUtils.deleteFile(tempFilePath);
        }

    }

    public static void main(String[] args) {
        splitFile();
    }

    public static void splitFile() {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("E:\\_ck\\素材\\test.mp4");
            //定义一次写入 5M 数据
            byte buf[] = new byte[1024*1024*1];
            int len;
            int count=1;
            fos = new FileOutputStream("E:\\_ck\\素材\\"+(count++)+".part");
            //用来记录程序运行中实时的每个 output 写入次数
            int num = 1;
            while ((len=fis.read(buf))!=-1){
                fos.write(buf,0,len);
                //如果写入次数大于 20(单个文件超过 100M),则此流停止,创建新的输出流写入新的片段
                if(num>5) {
                    fos.close();
                    fos = new FileOutputStream("E:\\_ck\\素材\\"+(count++)+".part");
                    //重新计数
                    num = 1;
                }
                else{
                    num++;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                fis.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        System.out.println("切割完毕!");
    }

}
